home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / CLESSONS.ZIP / LESSON5 < prev    next >
Text File  |  1986-02-07  |  15KB  |  292 lines

  1. .NT
  2.  A NOTE ABOUT THE LESSONS in C 
  3. .b4-24
  4. .R5C4
  5. These were written while the author was ~Ilearning~N  the language and since
  6. .R6C4
  7. they  are  ~Ifree~N ( to  copy  and/or  distribute ) there  is  a money-back
  8. .R7C4
  9. guarantee on the accuracy of each and every statement in the lessons (!)
  10. .R9C4
  11. The  ~Idisplay~N  program was written ( in C ) in order to provide a vehicle
  12. .R10C4
  13. for displaying the lessons.
  14. .R12C5
  15. .B
  16. P.J.Ponzo
  17. .B
  18. Dept. of Applied Math
  19. .B
  20. Univ. of Waterloo
  21. .B
  22. Ontario N2L 3G1
  23. .K16,30
  24. PonzoTUTOR
  25. .WNT
  26.    let's try IF FOR a WHILE   
  27.  
  28. .R4C1
  29. ~b~Imain() {                                    /* what's in a name ?    */ ~N
  30. ~b~I    int i, j;                               /* a bunch of integers.  */ ~N
  31. ~b~I    char name[10];                          /* an array of 10 chars. */ ~N
  32. ~b~I    for (i=0; i<25; i++)                    /* print '\n', 25 times, */ ~N
  33. ~b~I        printf("\n");                       /* to clear the screen!  */ ~N
  34. ~b~I    printf("\n Type your name : ");         /* ask for a name.       */ ~N
  35. ~b~I    scanf("%s",&name);                      /* input the name.       */ ~N
  36. ~b~I    for (i=0, j=0; name[i] != '\0'; i++)  { /* a nice for-loop.      */ ~N
  37. ~b~I        if ( name[i] == 'e' )               /* check for an 'e'.     */ ~N
  38. ~b~I            j++;                            /* if so, increment j.   */ ~N
  39. ~b~I    }                                       /* end of for-loop       */ ~N
  40. ~b~I    printf("\n The letter e occurs %d times in %s",j,name);          */ ~N
  41. ~b~I}                                           /* end of main()         */ ~N
  42. .R17C1
  43.     Let's look at this program.
  44.     It's supposed to ask for a name, then print out the number of times
  45.     the letter ~Ie~N occurs in the name.
  46. .WR4C1
  47. ~Vmain() {                                    /* what's in a name ?    */ ~N
  48. ~V    int i, j, num;                          /* a bunch of integers.  */ ~N
  49. ~V    char name[10];                          /* an array of 10 chars. */ ~N
  50. .R17C1
  51.     This part is familiar. Note that we allow for a name of ~I9~N characters
  52.     since the 10th will be the terminating ~I'\0'~N  (remember?).            
  53.                                                                            
  54. .WR4C1
  55. ~b~Imain() {                                    /* what's in a name ?    */ ~N
  56. ~b~I    int i, j;                               /* a bunch of integers.  */ ~N
  57. ~b~I    char name[10];                          /* an array of 10 chars. */ ~N
  58. ~V    for (i=0; i<25; i++)                    /* print '\n', 25 times, */ ~N
  59. ~V        printf("\n");                       /* to clear the screen!  */ ~N
  60. .R17C1
  61.                                                                               
  62.                                                                               
  63.                                                                               
  64.                                                                               
  65.                                                                               
  66. .R17C1
  67.     We use a ~Ifor~N loop to print ~I25~N ~b~In~Newlines (thereby clearing
  68.     the screen ..not very elegent, but.. ).                                
  69.     The new thing here is ~V i++ ~N which, in ~IC~N, means ~Iincrement i~N.
  70. .WR7C1
  71. ~b~I    for (i=0; i<25; i++)                    /* print '\n', 25 times, */ ~N
  72. ~b~I        printf("\n");                       /* to clear the screen!  */ ~N
  73. ~V    printf("\n Type your name : ");         /* ask for a name.       */ ~N
  74. .R17C1
  75.                                                                               
  76.                                                                               
  77.                                                                               
  78.                                                                               
  79.                                                                               
  80. .R17C1
  81.     We now ask for your name by printing:                                    
  82.                                                                            
  83. ~r~I Type your name : ~N                                                   
  84. .WR9C1
  85. ~b~I    printf("\n Type your name : ");         /* ask for a name.       */ ~N
  86. ~V    scanf("%s",&name);                      /* input the name.       */ ~N
  87. .R17C1
  88.                                                                               
  89.                                                                               
  90.                                                                               
  91.                                                                               
  92.                                                                               
  93. .R17C1
  94.     Then we wait for the user to type in his/her name (ending with the       
  95.     ~IEnter~N key), and put this ~Istring~N at ~Imemory address~N ~V &name ~N.
  96.                                                                                
  97. .WR10C1
  98. ~b~I    scanf("%s",&name);                      /* input the name.       */ ~N
  99. ~b~I    for (~Fi=0, j=0~N~b~I; name[i] != '\0'; i++)  { /* a nice for-loop.      */ ~N
  100. .R17C1
  101.                                                                               
  102.                                                                               
  103.                                                                               
  104.                                                                               
  105.                                                                               
  106. .R17C1
  107.     Now we go through the ~b~Iname[]~N array, one character at-a-time, starting
  108.     with ~b~Ii=0~N (the first character is the ~Izero~Nth!). We will count the
  109.     number of times the letter ~Ie~N occurs and store this count in the variable
  110.     ~b~Ij~N, so we also initialize ~b~Ij=0~N, too! (Note the use of the ~ICOMMA~N
  111.     between ~b~Ii=0~N and ~b~Ij=0~N).
  112. .WR11C1
  113. ~b~I    for (i=0, j=0; ~Fname[i] != '\0'~N~b~I; i++)  { /* a nice for-loop.      */ ~N
  114. .R17C1
  115.                                                                                
  116.                                                                                
  117.                                                                                
  118.                                                                                
  119.                                                                                
  120. .R17C1
  121.     This ~Ifor-loop~N will continue so long as the ~b~Ii~Nth character in the
  122.     ~b~Iname[]~N array is not the ~INULL '\0'~N. (Note the construction ~F~I!=~N
  123.     which, in C, means ~INOT EQUAL~N).
  124. .WR11C1
  125. ~b~I    for (i=0, j=0; name[i] != '\0'; ~Fi++~N~b~I)  { /* a nice for-loop.      */ ~N
  126. .R17C1
  127.                                                                               
  128.                                                                               
  129.                                                                               
  130.                                                                               
  131.                                                                               
  132. .R17C1
  133.     Of course, each time we advance through the characters in the array
  134.     ~b~Iname[]~N we must increment ~b~Ii~N (until we reach the end).                          
  135.                                                                                
  136. .WR11C1
  137. ~b~I    for (i=0, j=0; name[i] != '\0'; i++)  ~F{~N~b~I /* a nice for-loop.      */ ~N
  138. .R17C1
  139.     ...our openers for the ~Ifor~N...                                       
  140.                                                                                
  141.                                                                                
  142. .WR12C1
  143. ~V        if ( name[i] ~F==~N~V 'e' )               /* check for an 'e'.     */ ~N
  144. .R17C1
  145.                                                                               
  146.                                                                               
  147.                                                                               
  148.                                                                               
  149.                                                                               
  150. .R17C1
  151.     And now we check the ~b~Ii~Nth character, ~b~Nname[i]~N, to see if its ~IEQUAL~N
  152.     to the letter ~Ie~N. (Note the curious way we check for ~F==~N).           
  153.     (Had we used ~b~Iname[i]='e'~N it would compile OK, but this actually     
  154.      ~Iassigns~N to ~b~Iname[i]~N the character ~I'e'~N rather than checking
  155.      for ~Iequality~I! ...and ~b~Iname~N would be ~IALL e~Ns).
  156. .WR12C1
  157. ~b~I        if ( name[i] == 'e' )               /* check for an 'e'.     */ ~N
  158. ~V            j++;                            /* if so, increment j.   */ ~N
  159. .R17C1
  160.                                                                                
  161.                                                                                
  162.                                                                                
  163.                                                                                
  164.                                                                                
  165. .R17C1
  166.     Now, ~Iif~N we find an ~I'e'~N, we increment ~b~Ij~N.
  167. .WR13C1
  168. ~b~I            j++;                            /* if so, increment j.   */ ~N
  169. ~b~I~F    }~N~b~I                                       /* end of for-loop       */ ~N
  170. .R17C1
  171.                                                                               
  172.                                                                               
  173.                                                                               
  174.                                                                               
  175.                                                                               
  176. .R17C1
  177.     ...and this ~b~I~F}~N ends the ~Ifor-loop~N.
  178. .WR11C1
  179. ~b~I    for (i=0, j=0; name[i] != '\0'; i++)  { /* a nice for-loop.      */ ~N
  180. .R14C1
  181. ~b~I    }                                       /* end of for-loop       */ ~N
  182. ~V    printf("\n The letter e occurs %d times in %s",j,name);          */ ~N
  183. .R17C1
  184.                                                                               
  185.                                                                               
  186.                                                                               
  187.                                                                               
  188.                                                                               
  189. .R17C1
  190.     ...and, after leaving the ~Ifor-loop~N, we print the results:
  191.  
  192. ~r~IThe letter e occurs ~N  ~r~I times in ~N
  193.                                
  194.                    ~Ij~N           ~Iname~N
  195.                    goes here.  goes here.
  196. .WN
  197. ~b~Imain() {                                    /* what's in a name ?    */ ~N
  198. ~b~I    int i, j;                               /* a bunch of integers.  */ ~N
  199. ~b~I    char name[10];                          /* an array of 10 chars. */ ~N
  200. ~b~I    for (i=0; i<25; i++)                    /* print '\n', 25 times, */ ~N
  201. ~b~I        printf("\n");                       /* to clear the screen!  */ ~N
  202. ~b~I    printf("\n Type your name : ");         /* ask for a name.       */ ~N
  203. ~b~I    scanf("%s",&name);                      /* input the name.       */ ~N
  204. ~b~I    for (i=0, j=0; name[i] != '\0'; i++)  { /* a nice for-loop.      */ ~N
  205. ~b~I        if ( name[i] == 'e' )               /* check for an 'e'.     */ ~N
  206. ~b~I            j++;                            /* if so, increment j.   */ ~N
  207. ~b~I    }                                       /* end of for-loop       */ ~N
  208. ~b~I    printf("\n The letter e occurs %d times in %s",j,name);          */ ~N
  209. ~b~I}                                           /* end of main()         */ ~N
  210.     After we ~Icompile~N and ~Ilink~N and ~Iexe~Ncute, we get:
  211.  
  212. ~r~I Type your name : ~N ~IPeter~N
  213.                    
  214.                    we type this, then press the Enter key, and get:
  215.  
  216. ~r~IThe letter e occurs 2 times in Peter~N
  217. .WK9,30
  218. nice!nice!
  219. .WNT
  220.     IF (this ) do that ELSE IF (this) do that ELSE do that   
  221. .R4C1
  222.     We may wish to check for several characters (not just 'e'), so we could say:
  223.  
  224. 1 ~b~I        if ( name[i] == 'e' )       j++;    /* increment j if an 'e' */~N
  225. 2 ~b~I        else if ( name[i] == 'f' )  k++;    /* increment k if an 'f' */~N
  226. 3 ~b~I        else if ( name[i] == 'g' )  l++;    /* increment l if an 'g' */~N
  227. 4 ~b~I        else ;                              /* else do nothing       */~N
  228.  
  229.     In Line 1 we increment the variable ~b~Ij~N (which counts the number of times
  230.     an 'e' occurs.
  231.     In Line 2 we increment the variable ~b~Ik~N (which counts the number of times
  232.     an 'f' occurs.
  233.     In Line 3 we increment the variable ~b~Il~N (which counts the number of times
  234.     an 'g' occurs.
  235.     In Line 4 (which is reached only if the character is none of the above) we
  236.     do nothing.  (We could have done something interesting, but we should, just
  237.     once, demonstrate a DO NOTHING statement .... just the !@#$$% SEMI-COLON!).
  238.  
  239.     (We would, of course, have declared ~b~Ik~N and ~b~Il~N as ~b~Iint~N data types).
  240. .WK10,32
  241. of course!
  242. .WNT
  243.    More stuff like i++   
  244.     Although we could have incremented ~b~Ii~N by using ~b~Ii=i+1~N, we used
  245.     the ~Iincrement operator ++~N. There is also (what else?) a ~Idecrement~N
  246.     ~Ioperator --~N. In fact these can be either ~Ipre-~N or ~Ipost-operative~N.
  247.  
  248. ~b~Ij=i--~N   will assign to ~b~Ij~N the value of ~b~Ii~N, ~Ithen~N will 
  249.         decrement ~b~Ii~N.
  250. ~b~Ij=--i~N   will first decrement ~b~Ii~N, ~Ithen~N assign to ~b~Ij~N the decremented
  251.         value of ~b~Ii~N.
  252.  
  253.     Note the convenience of typing ~b~Iantidisestablishmentarianism++~N and not
  254.        ~b~Iantidisestablishmentarianism=antidisestablishmentarianism+1~N.
  255.  
  256.     Also, the following ~Iassignment operators~N may be used:
  257.  
  258. ~b~Ix+=5~N       instead of   ~b~Ix=x+5~N
  259. ~b~Ix-=5~N       instead of   ~b~Ix=x-5~N
  260. ~b~Ix*=5~N       instead of   ~b~Ix=x*5~N
  261. ~b~Ix/=5~N       instead of   ~b~Ix=x/5~N
  262. .WNT
  263.     and tests for EQUALITY && INEQUALITY etc.  
  264. .R5C1
  265.     To test for equality of, say, ~b~Ix~N and ~b~I5~N we ask if ~b~Ix==5~N.
  266.     Had we used something like: ~b~Iif (x=5)~N then ~b~Ix~N would be assigned
  267.     the value ~b~I5~N (and, of course, ~b~Ix~N would now BE equal to ~b~I5~N and
  268.     the if-statements would certainly be executed).
  269.  
  270.     We also use ~b~Iif (x!=5)~N (for NOT EQUAL) and ~b~Iif (x>5)~N and ~b~Iif (x<5)~N.
  271.  
  272.     We also have: ~b~Iif (x>5 && x!=7)~N where ~b~I&&~N means AND ...so this reads:
  273.     if (x is GREATER than 5) AND (x is NOT EQUAL to 7)
  274. .R15C1
  275.     We also have: ~b~Iif (x=5 ││ x>=7)~N where ~b~I││~N means OR  ...so this reads:
  276.     if (x is EQUAL to 5) OR (x is GREATER or EQUAL to 7) 
  277. .WR15C1
  278.     We also have: ~b~Iif (x~F=~N
  279. .K19,60
  280. x==5 !!!
  281. .WN
  282.  
  283.  
  284. .T
  285.    && that's all folks!   
  286. .K16,32
  287. au revoir!
  288.  
  289.  
  290. .q
  291.  
  292.